import {
  TYPE_COPPER,
  TYPE_SOLDERMASK,
  TYPE_SILKSCREEN,
  TYPE_SOLDERPASTE,
  TYPE_DRILL,
  TYPE_OUTLINE,
  TYPE_DRAWING,
  SIDE_TOP,
  SIDE_BOTTOM,
  SIDE_INNER,
  SIDE_ALL,
  CAD_KICAD,
  CAD_ALTIUM,
  CAD_ALLEGRO,
  CAD_EAGLE,
  CAD_EAGLE_LEGACY,
  CAD_EAGLE_OSHPARK,
  CAD_EAGLE_PCBNG,
  CAD_GEDA_PCB,
  CAD_ORCAD,
  CAD_DIPTRACE,
} from './constants'

import type {LayerType} from './types'

export const layerTypes: LayerType[] = [
  // High-priority non-matches
  {
    type: undefined,
    side: undefined,
    matchers: [
      // Eagle gerber generation metadata
      {
        ext: 'gpi',
        cad: [CAD_EAGLE, CAD_EAGLE_LEGACY, CAD_EAGLE_OSHPARK, CAD_EAGLE_PCBNG],
      },
      // Eagle drill generation metadata
      {
        ext: 'dri',
        cad: [CAD_EAGLE, CAD_EAGLE_LEGACY, CAD_EAGLE_OSHPARK, CAD_EAGLE_PCBNG],
      },
      // General data/BOM files
      {ext: 'csv', cad: undefined},
      // Pick-n-place BOMs
      {match: /pnp_bom/, cad: CAD_EAGLE_PCBNG},
    ],
  },
  {
    type: TYPE_COPPER,
    side: SIDE_TOP,
    matchers: [
      {ext: 'cmp', cad: CAD_EAGLE_LEGACY},
      {ext: 'top', cad: [CAD_EAGLE_LEGACY, CAD_ORCAD]},
      {ext: 'gtl', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'toplayer\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /top\.\w+$/, cad: [CAD_GEDA_PCB, CAD_DIPTRACE]},
      {match: /f[._]cu/, cad: CAD_KICAD},
      {match: /copper_top/, cad: CAD_EAGLE},
      {match: /top_copper/, cad: CAD_EAGLE_PCBNG},
      {match: /top copper/, cad: undefined},
    ],
  },
  {
    type: TYPE_SOLDERMASK,
    side: SIDE_TOP,
    matchers: [
      {ext: 'stc', cad: CAD_EAGLE_LEGACY},
      {ext: 'tsm', cad: CAD_EAGLE_LEGACY},
      {ext: 'gts', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'smt', cad: CAD_ORCAD},
      {ext: 'topsoldermask\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /topmask\.\w+$/, cad: [CAD_GEDA_PCB, CAD_DIPTRACE]},
      {match: /f[._]mask/, cad: CAD_KICAD},
      {match: /soldermask_top/, cad: CAD_EAGLE},
      {match: /top_mask/, cad: CAD_EAGLE_PCBNG},
      {match: /top solder resist/, cad: undefined},
    ],
  },
  {
    type: TYPE_SILKSCREEN,
    side: SIDE_TOP,
    matchers: [
      {ext: 'plc', cad: CAD_EAGLE_LEGACY},
      {ext: 'tsk', cad: CAD_EAGLE_LEGACY},
      {ext: 'gto', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'sst', cad: CAD_ORCAD},
      {ext: 'topsilkscreen\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /topsilk\.\w+$/, cad: [CAD_GEDA_PCB, CAD_DIPTRACE]},
      {match: /f[._]silks/, cad: CAD_KICAD},
      {match: /silkscreen_top/, cad: CAD_EAGLE},
      {match: /top_silk/, cad: CAD_EAGLE_PCBNG},
      {match: /top silk screen/, cad: undefined},
    ],
  },
  {
    type: TYPE_SOLDERPASTE,
    side: SIDE_TOP,
    matchers: [
      {ext: 'crc', cad: CAD_EAGLE_LEGACY},
      {ext: 'tsp', cad: CAD_EAGLE_LEGACY},
      {ext: 'gtp', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'spt', cad: CAD_ORCAD},
      {ext: 'tcream\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /toppaste\.\w+$/, cad: [CAD_GEDA_PCB, CAD_DIPTRACE]},
      {match: /f[._]paste/, cad: CAD_KICAD},
      {match: /solderpaste_top/, cad: CAD_EAGLE},
      {match: /top_paste/, cad: CAD_EAGLE_PCBNG},
    ],
  },
  {
    type: TYPE_COPPER,
    side: SIDE_BOTTOM,
    matchers: [
      {ext: 'sol', cad: CAD_EAGLE_LEGACY},
      {ext: 'bot', cad: [CAD_EAGLE_LEGACY, CAD_ORCAD]},
      {ext: 'gbl', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'bottomlayer\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /bottom\.\w+$/, cad: [CAD_GEDA_PCB, CAD_DIPTRACE]},
      {match: /b[._]cu/, cad: CAD_KICAD},
      {match: /copper_bottom/, cad: CAD_EAGLE},
      {match: /bottom_copper/, cad: CAD_EAGLE_PCBNG},
      {match: /bottom copper/, cad: undefined},
    ],
  },
  {
    type: TYPE_SOLDERMASK,
    side: SIDE_BOTTOM,
    matchers: [
      {ext: 'sts', cad: CAD_EAGLE_LEGACY},
      {ext: 'bsm', cad: CAD_EAGLE_LEGACY},
      {ext: 'gbs', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'smb', cad: CAD_ORCAD},
      {ext: 'bottomsoldermask\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /bottommask\.\w+$/, cad: [CAD_GEDA_PCB, CAD_DIPTRACE]},
      {match: /b[._]mask/, cad: CAD_KICAD},
      {match: /soldermask_bottom/, cad: CAD_EAGLE},
      {match: /bottom_mask/, cad: CAD_EAGLE_PCBNG},
      {match: /bottom solder resist/, cad: undefined},
    ],
  },
  {
    type: TYPE_SILKSCREEN,
    side: SIDE_BOTTOM,
    matchers: [
      {ext: 'pls', cad: CAD_EAGLE_LEGACY},
      {ext: 'bsk', cad: CAD_EAGLE_LEGACY},
      {ext: 'gbo', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'ssb', cad: CAD_ORCAD},
      {ext: 'bottomsilkscreen\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /bottomsilk\.\w+$/, cad: [CAD_GEDA_PCB, CAD_DIPTRACE]},
      {match: /b[._]silks/, cad: CAD_KICAD},
      {match: /silkscreen_bottom/, cad: CAD_EAGLE},
      {match: /bottom_silk/, cad: CAD_EAGLE_PCBNG},
      {match: /bottom silk screen/, cad: undefined},
    ],
  },
  {
    type: TYPE_SOLDERPASTE,
    side: SIDE_BOTTOM,
    matchers: [
      {ext: 'crs', cad: CAD_EAGLE_LEGACY},
      {ext: 'bsp', cad: CAD_EAGLE_LEGACY},
      {ext: 'gbp', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'spb', cad: CAD_ORCAD},
      {ext: 'bcream\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /bottompaste\.\w+$/, cad: [CAD_GEDA_PCB, CAD_DIPTRACE]},
      {match: /b[._]paste/, cad: CAD_KICAD},
      {match: /solderpaste_bottom/, cad: CAD_EAGLE},
      {match: /bottom_paste/, cad: CAD_EAGLE_PCBNG},
    ],
  },
  {
    type: TYPE_COPPER,
    side: SIDE_INNER,
    matchers: [
      {ext: 'ly\\d+', cad: CAD_EAGLE_LEGACY},
      {ext: 'gp?\\d+', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'in\\d+', cad: CAD_ORCAD},
      {ext: 'internalplane\\d+\\.ger', cad: CAD_EAGLE_OSHPARK},
      {match: /in(?:ner)?\d+[._]cu/, cad: CAD_KICAD},
      {match: /inner/, cad: CAD_DIPTRACE},
    ],
  },
  {
    type: TYPE_OUTLINE,
    side: SIDE_ALL,
    matchers: [
      {ext: 'dim', cad: CAD_EAGLE_LEGACY},
      {ext: 'mil', cad: CAD_EAGLE_LEGACY},
      {ext: 'gml', cad: CAD_EAGLE_LEGACY},
      {ext: 'gm\\d+', cad: [CAD_KICAD, CAD_ALTIUM]},
      {ext: 'gko', cad: CAD_ALTIUM},
      {ext: 'fab', cad: CAD_ORCAD},
      {ext: 'drd', cad: CAD_ORCAD},
      {match: /outline/, cad: [CAD_GEDA_PCB, CAD_EAGLE_PCBNG]},
      {match: /boardoutline/, cad: [CAD_EAGLE_OSHPARK, CAD_DIPTRACE]},
      {match: /edge[._]cuts/, cad: CAD_KICAD},
      {match: /profile/, cad: CAD_EAGLE},
      {match: /mechanical \d+/, cad: undefined},
    ],
  },
  {
    type: TYPE_DRILL,
    side: SIDE_ALL,
    matchers: [
      {ext: 'txt', cad: [CAD_EAGLE_LEGACY, CAD_ALTIUM]},
      {
        ext: 'xln',
        cad: [CAD_EAGLE, CAD_EAGLE_LEGACY, CAD_EAGLE_OSHPARK],
      },
      {ext: 'exc', cad: CAD_EAGLE_LEGACY},
      {ext: 'drd', cad: CAD_EAGLE_LEGACY},
      {ext: 'drl', cad: [CAD_KICAD, CAD_DIPTRACE]},
      {ext: 'tap', cad: CAD_ORCAD},
      {ext: 'npt', cad: CAD_ORCAD},
      {ext: 'plated-drill\\.cnc', cad: CAD_GEDA_PCB},
      {match: /fab/, cad: CAD_GEDA_PCB},
      {match: /npth/, cad: CAD_KICAD},
      {match: /drill/, cad: CAD_EAGLE_PCBNG},
    ],
  },
  {
    type: TYPE_DRAWING,
    side: undefined,
    matchers: [
      {ext: 'pos', cad: CAD_KICAD},
      {ext: 'art', cad: CAD_ALLEGRO},
      {ext: 'gbr', cad: undefined},
      {ext: 'gbx', cad: undefined},
      {ext: 'ger', cad: undefined},
      {ext: 'pho', cad: undefined},
    ],
  },
]
